package org.javabasics.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * Created by LUOLIANG on 2016/9/23.
 * 实现Callable接口创建多线程
 */
public class ThirdThread implements Callable<Integer> {
    private int i;

    @Override
    public Integer call() throws Exception {
        for (; i < 10; i++) {
            System.out.println("run方法：" + Thread.currentThread().getName() + " " + i);
        }

        return i;
    }

    public static void main(String[] args) {
        ThirdThread th = new ThirdThread();
        FutureTask<Integer> ft = new FutureTask<Integer>(th);
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            if (i == 5) {
                new Thread(ft, "有返回值的线程").start();
            }
        }
        try {
            //获取线程的返回值
            System.out.println("子线程的返回值：" + ft.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
